package www.wmx.com.xssc.util.threeUtil;

import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import www.wmx.com.xssc.domain.doc.jsfmj.*;
import www.wmx.com.xssc.domain.vo.extract.BaseMatchResult;
import www.wmx.com.xssc.domain.vo.extract.request.LWRequest;
import www.wmx.com.xssc.domain.vo.extract.request.PatentRequest;
import www.wmx.com.xssc.domain.vo.extract.request.TSecretRequest;
import www.wmx.com.xssc.domain.vo.extract.request.ZZRequest;
import www.wmx.com.xssc.domain.vo.response.JSFMResponse;
import www.wmx.com.xssc.service.jsfmj.*;
import www.wmx.com.xssc.util.ArticleProcessUtil;
import www.wmx.com.xssc.util.PatentProcessUtil;
import www.wmx.com.xssc.util.TSecretProcessUtil;
import www.wmx.com.xssc.util.ZZProcessUtil;
import www.wmx.com.xssc.util.enumUtil.CgjlType;
import www.wmx.com.xssc.util.time.DateFormatterUtil;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 技术发明奖根据奖项的类别：
 *  判别专利的数量需求
 */
@Data
@Component
@Slf4j
public class JsfmjProcessUtil {


    @Resource(description = "技术发明奖-软著-RbtKjjlJsfmjYdjjsjrjzzqzm")
    private IRbtKjjlJsfmjYdjjsjrjzzqzmService jsfmjYdjjsjrjzzqzmService;
    @Resource(description = "技术发明奖-论文-RbtKjjlJsfmjFblwjzzzm")
    private IRbtKjjlJsfmjFblwjzzzmService jsfmjFblwjzzzmService;
    @Resource(description = "技术发明奖-专利-RbtKjjlJsfmjFmzlzm")
    private IRbtKjjlJsfmjFmzlzmService jsfmjFmzlzmService;
    @Resource(description = "技术发明奖-技术秘密-RbtKjjlJsfmjJsmm")
    private IRbtKjjlJsfmjJsmmService jsfmjJsmmService;

    @Resource(description = "技术发明奖的基本信息")
    private IRbtKjjlJsfmjJbxxService jsfmjJbxxService;

    @Resource(description = "论文处理工具")
    private ArticleProcessUtil articleProcessUtil;
    @Resource(description = "专利处理工具")
    private PatentProcessUtil patentProcessUtil;
    @Resource(description = "专著处理工具")
    private ZZProcessUtil zzProcessUtil;
    @Resource(description = "技术秘密处理工具")
    private TSecretProcessUtil tSecretProcessUtil;

    @Resource(description = "技术发明奖成果登记")
    private IRbtKjjlJsfmjBjcgService jsfmjBjcgService;

    public JSFMResponse processJsfmj(String jbxxID) {

        boolean flag = true;
        JSFMResponse jsfmResponse = new JSFMResponse();
        List<LWRequest> lw = new ArrayList<>();
        List<PatentRequest> zl = new ArrayList<>();
        List<ZZRequest> zz = new ArrayList<>();
        List<TSecretRequest> tSecret = new ArrayList<>();

        RbtKjjlJsfmjJbxx jbxx = jsfmjJbxxService.lambdaQuery()
                .eq(RbtKjjlJsfmjJbxx::getId, jbxxID)
                .one();


        //0.成果登记：
        String cgdjFailReson = null;
        List<RbtKjjlJsfmjBjcg> jcyjjBjcgList = jsfmjBjcgService.lambdaQuery()
                .in(RbtKjjlJsfmjBjcg::getCgglDjId, jbxx.getCgglDjId().split(",，"))
                .list();
        for (RbtKjjlJsfmjBjcg jcyjjBjcg : jcyjjBjcgList) {
            if(!(jcyjjBjcg!=null &&  jcyjjBjcg.getKjjlJsfmjId()!=null && jcyjjBjcg.getKjjlJsfmjId().equals(jbxx.getId()))){
                cgdjFailReson = "项目成果未完成成果登记";
                jsfmResponse.setCgdjFailReson(cgdjFailReson);
                flag = false;
                break;
            }
        }
        //1.论文：
        List<RbtKjjlJsfmjFblwjzzzm> jcyjjLwxxList = jsfmjFblwjzzzmService.lambdaQuery()
                .eq(RbtKjjlJsfmjFblwjzzzm::getKjjlJsfmjId, jbxxID)
                .list();
        if (jcyjjLwxxList != null && !jcyjjLwxxList.isEmpty()) {
            articleProcessUtil.init();
            for (RbtKjjlJsfmjFblwjzzzm lwxx : jcyjjLwxxList) {
                LWRequest lwRequest = initLwRequest(jbxx, lwxx);
                BaseMatchResult result = articleProcessUtil.matchProcess(lwRequest);
                if (!result.isSuccess()) {
                    lwRequest.setFailReason(result.getFailReason());
                    lw.add(lwRequest);
                    flag = false;
                }
            }
        }
        //2.专利：
        List<RbtKjjlJsfmjFmzlzm> jcyjjFmzlzmList = jsfmjFmzlzmService.lambdaQuery()
                .eq(RbtKjjlJsfmjFmzlzm::getKjjlJsfmjId, jbxxID)
                .list();
        if (jcyjjFmzlzmList != null && !jcyjjFmzlzmList.isEmpty()) {
            patentProcessUtil.init();
            for (RbtKjjlJsfmjFmzlzm fmzlzm : jcyjjFmzlzmList) {
                PatentRequest patentRequest = initPatentRequest(jbxx, fmzlzm);
                BaseMatchResult result = patentProcessUtil.matchProcess(patentRequest);
                if (!result.isSuccess()) {
                    patentRequest.setFailReason(result.getFailReason());
                    zl.add(patentRequest);
                    flag = false;
                }
            }
        }
        //3.专著：
        List<RbtKjjlJsfmjYdjjsjrjzzqzm> jcyjjZzxxList = jsfmjYdjjsjrjzzqzmService.lambdaQuery()
                .eq(RbtKjjlJsfmjYdjjsjrjzzqzm::getKjjlJsfmjId, jbxxID)
                .list();
        if (jcyjjZzxxList != null && !jcyjjZzxxList.isEmpty()) {
            zzProcessUtil.init();
            for (RbtKjjlJsfmjYdjjsjrjzzqzm zzxx : jcyjjZzxxList) {
                ZZRequest zzRequest = initZZRequest(jbxx, zzxx);
                BaseMatchResult result = zzProcessUtil.matchProcess(zzRequest);
                if (!result.isSuccess()) {
                    zzRequest.setFailReason(result.getFailReason());
                    zz.add(zzRequest);
                    flag = false;
                }
            }
        }
        //4. 技术秘密：
        List<RbtKjjlJsfmjJsmm> jcyjjJsmmList = jsfmjJsmmService.lambdaQuery()
                .eq(RbtKjjlJsfmjJsmm::getKjjlJsfmjId, jbxxID)
                .list();
        if (jcyjjJsmmList != null && !jcyjjJsmmList.isEmpty()) {
            tSecretProcessUtil.init();
            for (RbtKjjlJsfmjJsmm jsmm : jcyjjJsmmList) {
                TSecretRequest tSecretRequest = initTSecretRequest(jbxx, jsmm);
                BaseMatchResult result = tSecretProcessUtil.matchProcess(tSecretRequest);
                if (!result.isSuccess()) {
                    tSecretRequest.setFailReason(result.getFailReason());
                    tSecret.add(tSecretRequest);
                    flag = false;
                }
            }
        }
        jsfmResponse.setPatentCount(jcyjjFmzlzmList!=null && !jcyjjFmzlzmList.isEmpty()?jcyjjFmzlzmList.size():0);
        jsfmResponse.setLwRequestList(lw);
        jsfmResponse.setZlRequestList(zl);
        jsfmResponse.setZzRequestList(zz);
        jsfmResponse.setSuccess(flag);
        return jsfmResponse;
    }


    private TSecretRequest initTSecretRequest(RbtKjjlJsfmjJbxx jsfmjJbxx, RbtKjjlJsfmjJsmm jsmm){
        LocalDateTime start = DateFormatterUtil.StrToLocalDateTime(jsfmjJbxx.getXmzqq());
        LocalDateTime end = DateFormatterUtil.StrToLocalDateTime(jsfmjJbxx.getXmzqz());
        List<String> names = Arrays.stream(jsmm.getQlr().split("、,，；;")).collect(Collectors.toList());
        TSecretRequest tSecretRequest = TSecretRequest.builder()
                .qlr(names)
                .mc(jsmm.getJsmmmc())
                .rdbm(jsmm.getRdbm())
                .build();
        tSecretRequest.setNd(jsfmjJbxx.getNd());
        tSecretRequest.setXmmc(jsfmjJbxx.getXmmc());
        tSecretRequest.setJbxxID(jsfmjJbxx.getId());
        tSecretRequest.setCgjlType(CgjlType.KJJBJ.getCode());
        tSecretRequest.setXmBeginTime(start);
        tSecretRequest.setXmEndTime(end);
        return tSecretRequest;
    }





    private LWRequest initLwRequest(RbtKjjlJsfmjJbxx jcyjjJbxx, RbtKjjlJsfmjFblwjzzzm lwxx) {
        LocalDateTime start = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqq());
        LocalDateTime end = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqz());
        List<String> names = Arrays.stream(lwxx.getZz().split("、,，；;")).collect(Collectors.toList());
        LWRequest lwRequest = LWRequest.builder()
                .authorEnroll(names)
                .lwtm(lwxx.getLwzzmc())
                .qkmc(lwxx.getFbkwcbs())
                .fbny(LocalDate.from(DateFormatterUtil.StrToLocalDateTime(lwxx.getFbcbsj())))
//                .qtzz(lwxx.getQtzz()) 作者
//                .zzpm(0) 作者排名
//                .sciTycs(lwxx.getSciTycs()) 他引次数
//                .yxyz(lwxx.getYxyz()) 影响因子
                .build();
        lwRequest.setNd(jcyjjJbxx.getNd());
        lwRequest.setXmmc(jcyjjJbxx.getXmmc());
        lwRequest.setJbxxID(jcyjjJbxx.getId());
        lwRequest.setCgjlType(CgjlType.KJJBJ.getCode());
        lwRequest.setXmBeginTime(start);
        lwRequest.setXmEndTime(end);
        return lwRequest;
    }

    private PatentRequest initPatentRequest(RbtKjjlJsfmjJbxx jcyjjJbxx, RbtKjjlJsfmjFmzlzm fmzlzm) {
        LocalDateTime start = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqq());
        LocalDateTime end = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqz());
        PatentRequest patentRequest = PatentRequest
                .builder()
                .zlmc(fmzlzm.getZlmc())
                .fmr(fmzlzm.getFmr())
                .fmrList(Arrays.stream(fmzlzm.getFmr().split("、,，；;")).collect(Collectors.toList()))
                .zlh(fmzlzm.getSqh())
                .build();
        patentRequest.setNd(jcyjjJbxx.getNd());
        patentRequest.setXmmc(jcyjjJbxx.getXmmc());
        patentRequest.setJbxxID(jcyjjJbxx.getId());
        patentRequest.setCgjlType(CgjlType.KJJBJ.getCode());
        patentRequest.setXmBeginTime(start);
        patentRequest.setXmEndTime(end);
        return patentRequest;
    }

    private ZZRequest initZZRequest(RbtKjjlJsfmjJbxx jcyjjJbxx, RbtKjjlJsfmjYdjjsjrjzzqzm zzxx) {
        LocalDateTime start = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqq());
        LocalDateTime end = DateFormatterUtil.StrToLocalDateTime(jcyjjJbxx.getXmzqz());
        ZZRequest zzRequest = ZZRequest
                .builder()
                .zzmc(zzxx.getJsjrjzzqmc())
//                .sbsj(zzxx.getSbsj())
                .fbsj(LocalDate.from(zzxx.getWcrq()))
                .zz(zzxx.getZzqr())
                .authorEnroll(Arrays.stream(zzxx.getZzqr().split("、,，；;")).collect(Collectors.toList()))
                .build();
        zzRequest.setNd(jcyjjJbxx.getNd());
        zzRequest.setXmmc(jcyjjJbxx.getXmmc());
        zzRequest.setJbxxID(jcyjjJbxx.getId());
        zzRequest.setCgjlType(CgjlType.KJJBJ.getCode());
        zzRequest.setXmBeginTime(start);
        zzRequest.setXmEndTime(end);
        return zzRequest;
    }


}
